<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .ipt-box {
            width: 100%;
            display: flex;
            margin: 20px 0;
        }

        .ipt-box input {
            width: 80%;
            height: 30px;
            padding-left: 10px;
        }

        .ipt-box button {
            margin-left: 10px;
            border: none;
            color: lightskyblue;
            background-color: white;
        }

        .state {
            display: flex;
            justify-content: space-around;
            line-height: 50px;
        }

        .state div {
            background-color: greenyellow;
            padding: 5px 10px;
            height: 30px;
            line-height: 30px;
            color: gray;
            border-radius: 5px;
        }

        .state .not {
            color: white;
            background-color: lightslategray;
        }

        .title {
            line-height: 50px;
            font-weight: 700;
        }

        .list .content {
            display: flex;
            justify-content: space-between;
            padding: 15px 10px;
            margin: 10px 0;
            background-color: greenyellow;
        }

        .list .change {
            background-color: lightslategray;
        }

        .null {
            color: red;
            text-align: center;
            margin-top: 30px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="ipt-box">
            <input type="text" placeholder="输入任务名称" v-model="value">
            <button @click="addlist">添加</button>
        </div>
        <div class="state">
            <div class="all">全部任务({{datalist.length}})</div>
            <div class="ok">已完成({{oknum}})</div>
            <div class="not">未完成({{unoknum}})</div>
        </div>
        <div class="title">任务列表</div>
        <div class="list">
            <div :class="{content:true ,change:item.state}" v-for="(item,index) in datalist" v-bind:key="index">
                <input type="checkbox" @click="changeState(item)">
                <div class="name">{{item.name}}</div>
                <div class="del" @click="del(item,index)">删除</div>
            </div>
        </div>
        <div class="null" v-show="datalist.length == 0">任务列表空空如也!</div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>

    new Vue({
        el: '#app',
        data: {
            value: '',
            datalist: [],
        },
        methods: {
            addlist() {
                if (this.value) {
                    var obj = { id: this.datalist.length + 1, name: this.value, state: false };
                    this.datalist.push(obj);
                    this.value = '';
                }
            },
            changeState(item) {
                item.state = !item.state;
            },
            del(item) {
                var i = this.datalist.findIndex((it) => { //返回找到的元素下标
                    return it.id == item.id
                })
                if (i != -1) {
                    this.datalist.splice(i, 1)
                }
            }
        },
        computed: {
            oknum() {
                return this.datalist.filter(function (value, index, self) {
                    return value.state;
                }).length;
            },
            unoknum() {
                return this.datalist.length - this.oknum;
            }
        }
    })

</script>